package com.leased.controller;

import com.leased.core.ActionContext;
import com.leased.core.AuthUser;
import com.leased.enums.BusinessStatusEnum;
import com.leased.exception.YCException;
import com.leased.handler.AdminInfoHandler;
import com.leased.pojo.AdminInfo;
import com.leased.request.BaseQuery;
import com.leased.request.admin.AdminInfoRequest;
import com.leased.response.AdminInfoResponse;
import com.leased.utils.APIResponse;
import com.leased.utils.Page;
import com.leased.utils.YCSystemStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by shencx on 2017/7/27.
 */
@Controller
@RequestMapping(value = "/api/admin")
public class AdminInfoController {

    final Logger LOG = LoggerFactory.getLogger(AdminInfoController.class);

    @Resource
    private AdminInfoHandler adminInfoHandler;

    /**
     * 管理员登录
     * @param request
     * @param adminInfo
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public APIResponse<AdminInfo> login(HttpServletRequest request,AdminInfo adminInfo){
        APIResponse<AdminInfo> apiResponse = new APIResponse<>();
        AdminInfo adminInfo1 = null;
        try {
            adminInfo1 = adminInfoHandler.adminLogin(adminInfo);
            if (adminInfo1 != null){
                AuthUser authUser = ActionContext.getActionContext().currentUser();
                authUser.login(Long.valueOf(adminInfo1.getId()),adminInfo1.getUserName(),adminInfo1.getRoleValue().getValue());
                apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getStatus());
                apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
                apiResponse.setData(adminInfo1);
            }else {
                apiResponse.setStatus(BusinessStatusEnum.EMPTY_RESULT.getCode());
                apiResponse.setMsg(BusinessStatusEnum.EMPTY_RESULT.getDesc());
            }
        } catch (Throwable e) {
            LOG.error("管理员登录发生异常",adminInfo);
            apiResponse.setStatus(BusinessStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public APIResponse login(HttpServletRequest request){
        APIResponse response = new APIResponse();
        AuthUser user = ActionContext.getActionContext().currentUser();
        user.logout();
        response.setStatus(0);
        return response;
    }

    /**
     * 获取管理员列表
     * @param request
     * @param baseQuery
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public APIResponse<Page<AdminInfoResponse>> list(HttpServletRequest request, BaseQuery baseQuery){
            APIResponse<Page<AdminInfoResponse>> apiResponse = new APIResponse<>();
            Page<AdminInfoResponse> page = null;
        try {
            page = adminInfoHandler.getAdminInfoPage(baseQuery);
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
            apiResponse.setData(page);
        } catch (Throwable e) {
            LOG.error("获取管理员列表发生异常", baseQuery);
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }

    /**
     * 添加修改管理员
     * @param request
     * @param adminInfoRequest
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public APIResponse edit(HttpServletRequest request, AdminInfoRequest adminInfoRequest){
        APIResponse apiResponse = new APIResponse();
        try {
            adminInfoHandler.addAdminInfo(adminInfoRequest);
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
        } catch (Throwable e) {
            LOG.error("添加修改管理员信息发生异常", adminInfoRequest);
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }

    /**
     * 更新管理员状态
     * @param request
     * @param id
     * @param status
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/modify",method = RequestMethod.POST)
    public APIResponse modify(HttpServletRequest request,Integer id,Integer status){
        APIResponse apiResponse = new APIResponse();
        try {
            adminInfoHandler.modifyStatus(id,status);
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
        } catch (Throwable e) {
            LOG.error("更新管理员状态发生异常", id,status);
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }

    /**
     * 获取详情
     * @param request
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    public APIResponse<AdminInfoResponse> detail(HttpServletRequest request, Integer id){
        APIResponse<AdminInfoResponse> apiResponse = new APIResponse<>();
        AdminInfoResponse adminInfoResponse = null;
        try {
            adminInfoResponse = adminInfoHandler.getAdminInfoById(id);
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
            apiResponse.setData(adminInfoResponse);
        } catch (Throwable e) {
            LOG.error("获取管理员详情信息发生异常", id);
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }
}
